package picobot.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JFileChooser;

import picobot.interfaces.core.ISimulator;
import picobot.rule.RuleBuilder;

/**
 * a class to perform a load rule action
 */
public class LoadRuleAction implements ActionListener {

	JFileChooser fc;
	ISimulator sim;

	/**
	 * the constructor for LoadRuleAction which takes a PicobotUserInterface
	 * 
	 * @param ui
	 *            is the PicobotUserInterface
	 */
	public LoadRuleAction(PicobotUserInterface ui) {
		sim = ui.getSimulator();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		fc = new JFileChooser();
		fc.setCurrentDirectory(new java.io.File("."));
		
		int returnVal = fc.showOpenDialog(fc);

		if (returnVal == JFileChooser.APPROVE_OPTION) {
			File file = fc.getSelectedFile();

			// load the selected file into the picobot of the simulation of the
			// PicobotUserInterface
			sim.getPicobot().loadRules(new RuleBuilder().parseRules(file));
		}

	}

}